package com.lin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.entity.Article;
import com.lin.entity.Statistic;
import com.lin.mapper.ArticleMapper;
import com.lin.mapper.CommentMapper;
import com.lin.mapper.StatisticMapper;
import com.lin.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 阿林
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo<Article> findArticleWithPage(Integer pageNum, Integer count) {
        PageHelper.startPage(pageNum, count);
        List<Article> all = articleMapper.findAll();
        return new PageInfo<>(all);
    }

    @Override
    public Article findById(Integer id) {
        //文章数据
        //先从redis缓存中获取
        Article article = (Article) redisTemplate.opsForValue().get("article_" + id);
        //如果缓存中没有,再从数据库查询
        //缓存中没有数据
        if (article == null) {
            //从数据获取
            article = articleMapper.findById(id);
            //查询完毕,保存到redis缓存中
            redisTemplate.opsForValue().set("article_" + id, article);
        }
        return article;
    }


    @Override
    public List<Article> getHeatArticles() {
        return articleMapper.getHeatArticles(10);

       /* //根据hits后去排名前10的Statistic (articleId,hits,commentNum)
        List<Statistic> statistics = statisticMapper.findHeatArticle();
        //创建一个集合,保存所有的热门文章
        List<Article> articles = new ArrayList<>();
        //获取对应的10个Article
        for (Statistic statistic : statistics) {
            //点击量
            Integer hits = statistic.getHits();
            //评论量
            Integer commentsNum = statistic.getCommentsNum();
            //文章id
            Integer articleId = statistic.getArticleId();
            //根据文章id获取具体的文章对象
            Article article = articleMapper.findById(articleId);
            article.setHits(hits);
            article.setCommentNum(commentsNum);
            //保存到集合中
            articles.add(article);
        }
        return articles;*/
    }

    @Override
    public Integer getTotalArticlesNum() {
        return articleMapper.getTotalArticlesNum();
    }

    @Override
    public List<Article> findRecentArticle(Integer num) {
        return articleMapper.findRecentArticle(num);

      /* List<Article> recentArticle = articleMapper.findRecentArticle(num);
       //遍历最新的文章
        for (Article article : recentArticle) {
            //获取文章id
            Integer id = article.getId();

            //根据文章id查询 statistic对象,封装hist 和 commentsNum
            Statistic statistic = statisticMapper.findByArticleId(id);
            //获取文章点击量
            Integer hits = statistic.getHits();
            //获取文章评论量
            Integer commentsNum = statistic.getCommentsNum();
            //将文章点击量 封装到 article对象里面
            article.setHits(hits);
            //将文章评论量 封装到 article对象里面
            article.setCommentNum(commentsNum);
        }
        //将赋值好的对象返回出去,
        return recentArticle;*/
    }

    /**
     * 发布文章
     *
     * @param article
     */
    @Override
    public void pubishArticle(Article article) {
        //1文章内容添加到t_article表格中
        //(1)封装数据
        article.setCreated(new Date());
        article.setModified(new Date());
        article.setCategories("默认类别");
        article.setTags("默认标签");
        article.setHits(0);
        article.setCommentNum(0);
        articleMapper.addArticle(article);
        //2 保存到文章统计表t_statistic中
        Statistic statistic = new Statistic();
        statistic.setArticleId(article.getId());
        statistic.setHits(0);
        statistic.setCommentsNum(0);
        statisticMapper.addStatistic(statistic);
    }


    @Override
    public void updateArticle(Article article) {
        // 设置 modify 修改的最新时间
        article.setModified(new Date());
        //修改 article 数据
        articleMapper.updateArticle(article);
        //清理  Redis缓存
        redisTemplate.delete("article_" + article.getId());

    }

    /**
     * 删除文章
     *
     * @param articleId
     */
    @Override
    public void deleteArticle(Integer articleId) {
        //从 t_article 删除数据
        articleMapper.deleteArticle(articleId);
        //从 t_comment 删除数据
        commentMapper.deleteCommentByArticleId(articleId);
        //从 t_statistic 删除数据
        statisticMapper.deleteByArticleId(articleId);
        //清理Redis缓存
        redisTemplate.delete("article_" + articleId);

    }


}
